/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.core;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Represents an attribute in a element like bgcolor or id.
 * @author J Patrick Davenport
 *
 */
public class Attribute {
	private String	name;
	private String	value;

	public Attribute(final String _name) {
		this.name = _name;
	}

	public Attribute(final String _name, final String _value) {
		this(_name);
		this.value = _value;
	}

	public String getName() {
		return this.name;
	}

	public String getValue() {
		return this.value;
	}

	public void setValue(final String _value) {
		this.value = _value;
	}

	/**
	 * The rendered version of the attribute.
	 * If the name parameter is empty, an empty string is returned.
	 * If the value is empty, but the name is not the attribute renders without a value and a trailing space.
	 * Rendered output example: title="My great title" .
	 */
	@Override
	public String toString() {
		if (StringUtils.isEmpty(this.name)) { return ""; }
		return this.name + "=\"" + (StringUtils.isEmpty(this.value) ? "" : this.value) + "\"";
	}

	@Override
	public boolean equals(final Object _obj) {
		if (_obj == null || !(_obj instanceof Attribute)) { return false; }
		final Attribute in = (Attribute) _obj;
		return new EqualsBuilder()
					.append(in.getName(), this.name)
					.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.name).toHashCode();
	}
}
